﻿using Engine.Graphics;
using Game.NetWork;
using GameEntitySystem;
using Test1;

namespace Game
{
    public class ComponentTest1Package : IPackage
    {
        public enum EventType
        {
            SyncStat,
            TurnToLowSen,
            TurnToHighSen,
            ZeroSen,
            SyncStat2,
            SyncMagic
        }

        public float Sen;

        public float rate;

        public bool isfirstspawnNet;

        public EventType m_eventType;

        public ushort entityId;

        public float mpNet;

        public int MplevelNet;

        public float m_MaxmpNet;

        public byte ID => 109;

        public Client To { get; set; }

        public Client Except { get; set; }

        public Client From { get; set; }

        public ClientState MinNeedState => ClientState.ProjectLoaded;

        public ComponentTest1Package()
        {
        }

        public ComponentTest1Package(ComponentTest1 flu, EventType eventType)
        {
            entityId = flu.Entity.EntityId;
            m_eventType = eventType;
            Sen = flu.m_sen;
        }

        public ComponentTest1Package(ComponentTest1 flu, EventType eventType, bool fs)
        {
            entityId = flu.Entity.EntityId;
            m_eventType = eventType;
            isfirstspawnNet = fs;
        }

        public ComponentTest1Package(ComponentTest1 flu, EventType eventType, int a)
        {
            entityId = flu.Entity.EntityId;
            m_eventType = eventType;
            mpNet = flu.m_mp;
            MplevelNet = flu.FCMagicLevel;
            m_MaxmpNet = flu.m_Maxmp;
        }

        public void WriteData(PackageStreamWriter writer)
        {
            writer.WriteEnum(m_eventType);
            switch (m_eventType)
            {
                case EventType.SyncStat2:
                    writer.Write(entityId);
                    writer.Write(isfirstspawnNet);
                    break;
                case EventType.SyncStat:
                    writer.Write(entityId);
                    writer.Write(Sen);
                    break;
                case EventType.TurnToLowSen:
                    writer.Write(entityId);
                    break;
                case EventType.TurnToHighSen:
                    writer.Write(entityId);
                    break;
                case EventType.SyncMagic:
                    writer.Write(entityId);
                    writer.Write(mpNet);
                    writer.Write(MplevelNet);
                    writer.Write(m_MaxmpNet);
                    break;
                case EventType.ZeroSen:
                    break;
            }
        }

        public void ReadData(PackageStreamReader reader)
        {
            m_eventType = reader.ReadEnum<EventType>();
            switch (m_eventType)
            {
                case EventType.SyncStat2:
                    entityId = reader.ReadUInt16();
                    isfirstspawnNet = reader.ReadBoolean();
                    break;
                case EventType.SyncStat:
                    entityId = reader.ReadUInt16();
                    Sen = reader.ReadSingle();
                    break;
                case EventType.TurnToLowSen:
                    entityId = reader.ReadUInt16();
                    break;
                case EventType.TurnToHighSen:
                    entityId = reader.ReadUInt16();
                    break;
                case EventType.SyncMagic:
                    entityId = reader.ReadUInt16();
                    mpNet = reader.ReadSingle();
                    MplevelNet = reader.ReadInt32();
                    m_MaxmpNet = reader.ReadSingle();
                    break;
                case EventType.ZeroSen:
                    break;
            }
        }

        public void Handle(ProjectNet projectNet, NetNode netNode, bool isServer)
        {
            switch (m_eventType)
            {
                case EventType.SyncStat:
                    projectNet.FindEntityById(entityId, delegate (Entity entity)
                    {
                        ComponentTest1 componentTest = entity.FindComponent<ComponentTest1>();
                        if (componentTest != null)
                        {
                            componentTest.m_sen = Sen;
                        }
                    });
                    break;
                case EventType.SyncStat2:
                    projectNet.FindEntityById(entityId, delegate (Entity entity)
                    {
                        ComponentTest1 componentTest2 = entity.FindComponent<ComponentTest1>();
                        if (componentTest2 != null)
                        {
                            componentTest2.isfirstspawn = isfirstspawnNet;
                        }
                    });
                    break;
                case EventType.TurnToLowSen:
                    projectNet.FindEntityById(entityId, delegate (Entity entity)
                    {
                        ComponentTest1 componentTest3 = entity.FindComponent<ComponentTest1>();
                        if (componentTest3 != null)
                        {
                            projectNet.FindSubsystem<SubsystemBlocksTexture>(throwOnError: true).BlocksTexture = ContentManager.Get<Texture2D>("BlocksSen");
                            componentTest3.UpdateAllChunks(0f, TerrainChunkState.InvalidLight);
                        }
                    });
                    break;
                case EventType.TurnToHighSen:
                    projectNet.FindEntityById(entityId, delegate (Entity entity)
                    {
                        ComponentTest1 componentTest4 = entity.FindComponent<ComponentTest1>();
                        if (componentTest4 != null)
                        {
                            projectNet.FindSubsystem<SubsystemBlocksTexture>(throwOnError: true).BlocksTexture = BlocksTexturesManager.DefaultBlocksTexture;
                            componentTest4.UpdateAllChunks(0f, TerrainChunkState.InvalidLight);
                        }
                    });
                    break;
                case EventType.SyncMagic:
                    projectNet.FindEntityById(entityId, delegate (Entity entity)
                    {
                        ComponentTest1 componentTest5 = entity.FindComponent<ComponentTest1>();
                        if (componentTest5 != null)
                        {
                            componentTest5.m_mp = mpNet;
                            componentTest5.FCMagicLevel = MplevelNet;
                            componentTest5.m_Maxmp = m_MaxmpNet;
                        }
                    });
                    break;
                case EventType.ZeroSen:
                    break;
            }
        }
    }
}